package jjanga.sqlite;

import java.util.ArrayList;

import org.w3c.dom.Text;

import jjanga.sqlite.FmFileDatabase.DBFileItem;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class sqliteClient extends Activity implements OnClickListener{
	Button mQueryButton;
	Button mUpdateQueryButton;
	Button mDeleteQueryButton;
	EditText mQueryText;
	TableLayout mQueryTable;
	ContentResolver mCr;
	FmFileDatabase mDB;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mQueryButton = (Button)findViewById(R.id.query_btn_select);
        mUpdateQueryButton = (Button)findViewById(R.id.query_btn_update);
        mDeleteQueryButton = (Button)findViewById(R.id.query_btn_delete);
        mQueryText = (EditText)findViewById(R.id.query_edit);
        mQueryTable	= (TableLayout)findViewById(R.id.query_table);        
        mQueryButton.setOnClickListener(this);
        mUpdateQueryButton.setOnClickListener(this);
        mDeleteQueryButton.setOnClickListener(this);
        mCr = this.getContentResolver();
        mDB = new FmFileDatabase(this);
    }
    
	@Override
	public void onClick(View a_oView) {
		if(a_oView == mQueryButton){
			mQueryTable.removeAllViews();
			String strWhere = mQueryText.getText().toString();
			ArrayList<DBFileItem> oItems = mDB.Query(strWhere);
			int nLength = oItems.size();
			for(int i=0;i<nLength;i++){
				Log.i("QUERY_DATA", oItems.get(i).strData);				
				DBFileItem oItem = oItems.get(i);
				TableRow oTableRow = new TableRow(this);
				TextView oId = new TextView(this);
				TextView oDataText = new TextView(this);
				TextView oParentText = new TextView(this);
				oId.setText(Long.toString(oItem.nID));
				oDataText.setText(oItem.strData);
				oParentText.setText(Long.toString(oItem.nParent));
				oTableRow.addView(oId);
				oTableRow.addView(oDataText);
				oTableRow.addView(oParentText);
				mQueryTable.addView(oTableRow);
			}
		}else if(a_oView == mUpdateQueryButton){
//			String strWhere = mQueryText.getText().toString();
			String strWhere = "_id='9'";
			ContentValues oValues = new ContentValues();
			oValues.put("_data", "/mnt/sdcard/DCIMM");
			mDB.UpdateQuery(oValues, strWhere);
		}else if(a_oView == mDeleteQueryButton){
			String strWhere = mQueryText.getText().toString();
			mDB.DeleteFileDatabase(strWhere);			
		}
	}
}